package utils;

import java.util.UUID;

public class UploadUtil {

    //获取真实文件名
    public static String getRealName(String name){
        //获取最后一个"/"
        int index = name.lastIndexOf("\\");
        return name.substring(index+1);
    }

    //获取随机文件名
    public static String getUUIDName(String realName){
        //realname  可能是  1.jpg   也可能是  1
        //获取后缀名
        int index = realName.lastIndexOf(".");
        if(index==-1){
            return UUID.randomUUID().toString().replace("-", "").toUpperCase();
        }else{
            return UUID.randomUUID().toString().replace("-", "").toUpperCase()+realName.substring(index);
        }
    }

    //获取文件夹目录
    public static String getDir(String name){
        int i = name.hashCode();
        String hex = Integer.toHexString(i);
        int j=hex.length();
        for(int k=0;k<8-j;k++){
            hex="0"+hex;
        }
        return "/"+hex.charAt(0)+"/"+hex.charAt(1);
    }

    public static void main(String[] args) {
            String s="D:\\大二下作业集合\\计算机组成原理\\12.png";
            String realName = getRealName(s);
            System.out.println(realName);

            String uuidName = getUUIDName(realName);
            System.out.println(uuidName);

            String dir = getDir(realName);
            System.out.println(dir);


    }
}
